/**
 *
 * @param time created_at 时间
 * @param need 需要分隔符
 * @returns
 */
export const handleTime = (time, need = false) => {
  if (typeof time != "string") {
    return null;
  }
  if (!time) {
    return null;
  }
  let date = new Date(time);
  const isDate = date instanceof Date;
  if (isDate) {
    let useTime = time.split(" ")[0],
      useTime1 = time.split(" ")[1],
      dateArr = useTime.split("-"),
      date1Arr = useTime1.split(":"),
      str = "";
    // str = dateArr[0] + "年" + dateArr[1] + "月" + dateArr[2] + "日";
    str = dateArr[0] + "/" + dateArr[1] + "/" + dateArr[2];
    if (need) {
      str +=
        (typeof need == "string" ? need : " ") +
        date1Arr[0] +
        "时" +
        date1Arr[1] +
        "分";
      // + date1Arr[2] + '秒'
    }
    return str;
  }

  return null;
};

export const getTime = () => {
  //获取时间 年月日
  const now = new Date(); // 创建一个 Date 对象，表示当前时间
  const year = now.getFullYear(); // 获取当前年份
  const month = (now.getMonth() + 1).toString().padStart(2, "0"); // 获取当前月份（注意：月份从 0 开始计数，所以需要加 1）
  const day = now.getDate().toString().padStart(2, "0"); // 获取当前日份
  return `${year}-${month}-${day}`;
};

/**
 *  函数防抖
 * @param {number} delay 默认1000毫秒
 * @param {Function} fn 执行函数
 */

export const debounce = (fn, delay = 1000) => {
  let timer = undefined;
  return function (...argu) {
    if (timer) {
      clearTimeout(timer);
    }
    timer = setTimeout(() => {
      fn.call(null, ...argu);
    }, delay);
  };
};

/**
 *  函数节流
 * @param {number} delay 默认100毫秒
 * @param {Function} func 执行函数
 */

export const throttle = (func, delay) => {
  var timer = null;
  return function (...args) {
    if (!timer) {
      timer = setTimeout(function () {
        func.apply(this, args);
        timer = null;
      }, delay);
    }
  };
};

/**
 *  @description 对象克隆
 *  @param {Object | Array} obj 需要克隆的对象或者数组
 */

export function deepClone(obj) {
  if (typeof obj !== "object" || obj === null) {
    return obj;
  }

  if (Array.isArray(obj)) {
    return obj.map(deepClone);
  }

  return Object.keys(obj).reduce((acc, key) => {
    acc[key] = deepClone(obj[key]);
    return acc;
  }, {});
}

/**
 * @param { String | Date } date
 * @description 日期格式化
 */
export function formatDate(date) {
  if (!date) {
    date = new Date();
  }
  const reg = /(\/)|((?<=\/)\d(?=\/|\s))/g;
  Object.prototype.toString.call(date) === "[object Date]" &&
    (date = date.toLocaleString());
  typeof date === "string" &&
    (date = date.replace(reg, ($1) => ($1 === "/" ? "-" : 0 + $1)));
  return date;
}

/**
 * @description 判断路由是否包含
 * @param {String} baseRoute
 * @param {String} targetRoute
 * @returns {Boolean}
 */
export function isRouteIncluded(baseRoute, targetRoute) {
  // 使用RegExp构造函数时，需要对字符串进行双重转义
  const pattern =
    "^" + baseRoute.replace(/\/$/, "").replace(/\//g, "\\/") + "(\\/|$)";
  const regex = new RegExp(pattern);
  return regex.test(targetRoute);
}

/**
 * @Description 路径拼接
 * @param {String} ...paths
 * @returns {String}
 */

export function joinPath(...paths) {
  // 使用 reduce 遍历所有路径部分，并进行拼接处理
  return paths
    .reduce((acc, part) => {
      if (part) {
        // 去除每个部分开头和结尾的斜杠
        const cleanedPart = part.replace(/^\/+|\/+$/g, "");
        // 拼接部分路径，并确保中间有一个斜杠
        if (cleanedPart) {
          return `${acc}/${cleanedPart}`;
        }
      }
      return acc;
    }, "")
    .replace(/^\/+/, "/"); // 确保最终路径以单个斜杠开头
}

/**
 * @description 筛选对象属性
 * @param {Object} obj
 * @param {Array} props
 * @returns {Object}
 */

export function pick(obj, ...props) {
  return Object.fromEntries(
    Object.entries(obj).filter(([key]) => props.includes(key))
  );
}
